import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
    const login = ref<boolean>(false);
    const token = ref<string>('');
    const username = ref<string>('')

    watch(login, newvalue => {
        localStorage.setItem("login", JSON.stringify(newvalue))
    })
    onMounted(() => {
        if (localStorage.getItem("login")) {
            login.value = JSON.parse(localStorage.getItem("login") as string)
        }
    })
    watch(username, newvalue => {
        localStorage.setItem("username", JSON.stringify(newvalue))
    })
    onMounted(() => {
        if (localStorage.getItem("username")) {
            username.value = JSON.parse(localStorage.getItem("username") as string)
        }
    })
    return { username, login, token }
})